package com.ontheroad.mysql.path.listener;

import com.ontheroad.mysql.path.event.PathEvent;
import com.ontheroad.mysql.path.mapper.PathUserMapper;
import com.ontheroad.path.model.Point;
import org.alternativevision.gpx.GPXParser;
import org.alternativevision.gpx.beans.GPX;
import org.alternativevision.gpx.beans.Track;
import org.alternativevision.gpx.beans.Waypoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by kedong on 2017/8/4 0004.
 */
@Service
public class PathListener implements ApplicationListener<PathEvent> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PathUserMapper mapper;

    @Override
    public void onApplicationEvent(PathEvent event) {
        try {
            Integer pathSysId = event.getPathSysId();
            Integer pathUserId = event.getPathUserId();
            Integer userId = event.getUserId();
            /**
             * 将轨迹点全部入库，这是为了查找附近的轨迹
             */
            GPXParser gpxParser = new GPXParser();
            GPX gpx = gpxParser.parseGPX(new ByteArrayInputStream(event.getGpxStr().getBytes("GBK")));
            // 获取轨迹
            Set<Track> tracks = gpx.getTracks();
            if (tracks == null || tracks.size() == 0) {
                return;
            }
            List<Point> points = new ArrayList<>();
            for (Track track : tracks) {
                // 获取轨迹点
                List<Waypoint> waypoints = track.getTrackPoints();
                if (waypoints.size() == 0) {
                    return;
                }
                for (Waypoint waypoint : waypoints) {
                    Point point = new Point();
                    point.setPathSysId(Long.valueOf(pathSysId));
                    point.setPathUserId(pathUserId);
                    // 纬度
                    point.setLat(waypoint.getLatitude());
                    // 经度
                    point.setLng(waypoint.getLongitude());
                    point.setName(waypoint.getName());
                    point.setComment(waypoint.getComment());
                    // 高度
                    point.setElevation(waypoint.getElevation());
                    point.setUserId(userId);
                    point.setDescription(waypoint.getDescription());
                    point.setTime(waypoint.getTime());
                    points.add(point);
                    // 每1000条数据入库一次
                    if (points.size() >= 1000) {
                        mapper.batchSavePoints(points);
                        points.clear();
                    }
                }
                if (points.size() > 0) {
                    mapper.batchSavePoints(points);
                }
            }
        } catch (Exception e) {
            logger.error("保存轨迹点失败", e);
        }
    }
}
